package com.example.steveaoblogdemo.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.steveaoblogdemo.entity.User;
import com.example.steveaoblogdemo.mapper.UserMapper;
import com.example.steveaoblogdemo.service.impl.UserServiceImpl;
import com.example.steveaoblogdemo.utils.ExportExcel;
import com.example.steveaoblogdemo.utils.ImgUpload;
import com.example.steveaoblogdemo.utils.Result;
import com.example.steveaoblogdemo.utils.SaltUtil;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;


@RestController
public class UserController {
    @Resource
    UserServiceImpl userService;

    @Resource
    UserMapper userMapper;

    @PostMapping("/user/login")
    public Result<?> login(@RequestBody User user){
        return userService.login(user);
    }


    @PostMapping("/user/adminLog")
    public Result<?> adminLog(@RequestParam(defaultValue = "")String username,@RequestParam(defaultValue = "")String password){
        return userService.adminLog(username,password);
    }

    @PostMapping("/user/log")
    public Result<?> log(@RequestParam(defaultValue = "")String username,@RequestParam(defaultValue = "")String password){
        return userService.log(username,password);
    }

    @PostMapping("/user/upload")
    public Result<?> uploadImg(@RequestParam("file")MultipartFile file){
        ImgUpload imgUpload = new ImgUpload();
        return imgUpload.uploadImg(file);
    }

    @GetMapping("/user/queryById")
    public Result<?> queryById(@RequestParam(required = false)Integer id){
        return userService.queryById(id);
    }

    @GetMapping("/user/queryUser")
    public Result<?> queryUser(@RequestParam(defaultValue = "1")Integer pageNum,@RequestParam(defaultValue = "10")Integer pageSize,@RequestParam(defaultValue = "")String search){
        return userService.queryUser(pageNum,pageSize,search);
    }

    @PutMapping("/user/updateUser")
    public int updateUser(@RequestParam(defaultValue = "") String username, @RequestParam(defaultValue = "") String nickname, @RequestParam(defaultValue = "") String password, @RequestParam(defaultValue = "") String avatar,
                          @RequestParam(defaultValue = "") String sex, @RequestParam(defaultValue = "") String email, @RequestParam(defaultValue = "") String resume, @RequestParam(required = false) Integer salt,@RequestParam(defaultValue = "0") Integer recommend,
                          @RequestParam(defaultValue = "0")Integer role,@RequestParam(required = false) Integer id){
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        User u = userMapper.selectOne(wrapper.eq(User::getPassword,password).eq(User::getSalt,salt));
        if (u!=null){
            return userService.updateUser(username,nickname,password,avatar,sex,email,resume,salt,recommend,role,id);
        }
        else {
            Integer saltForOne = SaltUtil.createSalt();
            String pwd = DigestUtils.md5Hex(password+saltForOne);
            return userService.updateUser(username,nickname,pwd,avatar,sex,email,resume,saltForOne,recommend,role,id);
        }
    }

    @PostMapping("/user/addUser")
    public int addUser(@RequestParam(required = false)Integer id,@RequestParam(defaultValue = "")String username,@RequestParam(defaultValue = "")String nickname,@RequestParam(defaultValue = "")String password,@RequestParam(defaultValue = "")String avatar,
                       @RequestParam(defaultValue = "")String sex,@RequestParam(defaultValue = "")String email,@RequestParam(defaultValue = "")String resume,@RequestParam(required = false) Integer salt,@RequestParam(defaultValue = "0")Integer recommend,
                       @RequestParam(defaultValue = "0")Integer role){
        Integer saltForOne = SaltUtil.createSalt();
        String pwd = DigestUtils.md5Hex(password+saltForOne);
        return userService.addUser(id,username,nickname,pwd,avatar,sex,email,resume,saltForOne,recommend,role);

    }

    @PutMapping("/user/deleteUser")
    public Result<?> deleteUser(@RequestParam(required = false)Integer id){
        return userService.deleteUser(id);
    }

    @GetMapping("/user/checkUsername")
    public Result<?> checkUsername(@RequestParam(defaultValue = "")String username){
        return userService.queryByName(username);
    }

    @GetMapping("/user/checkEmail")
    public Result<?> checkEmail(@RequestParam(defaultValue = "")String email){
        return userService.queryByEmail(email);
    }

    @PutMapping("/user/forgetMyPassword")
    public Result<?> forgetMyPassword(@RequestParam(defaultValue = "")String email,@RequestParam(defaultValue = "")String password){
        return userService.forgetMyPassword(email,password);
    }

    @PostMapping("/user/checkToken")
    public Boolean checkToken(HttpServletRequest request){
        return userService.checkToken(request);
    }

    @RequestMapping("/user/downloadUserInfo")
    public void exportExcel(HttpServletResponse response) throws Exception {
        List<User> userList = userService.getUserInfo();
        ExportExcel.downloadUnfilledToXlsx("用户表",response,User.class,userList);
    }
}
